package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.item.entity.ProductBase;
import cn.zysuyuan.item.entity.ProductBaseEdition;
import cn.zysuyuan.item.entity.ProductBaseImg;
import cn.zysuyuan.item.entity.ProductBaseImgEdition;
import cn.zysuyuan.item.mapper.MedicinalEditionMapper;
import cn.zysuyuan.item.mapper.ProductBaseEditionMapper;
import cn.zysuyuan.item.mapper.ProductBaseImgEditionMapper;
import cn.zysuyuan.item.service.ProductbaseEditionService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-02-18 13:01
 */

@Service
public class ProductbaseEditionServiceImpl implements ProductbaseEditionService {

    @Autowired
    private ProductBaseEditionMapper productBaseEditionMapper;

    @Autowired
    private MedicinalEditionMapper medicinalEditionMapper;

    @Autowired
    private ProductBaseImgEditionMapper productBaseImgEditionMapper;

    @Override
    public PageResult<ProductBaseEdition> selectList(Map<String, Object> map) {
        // 将省市区拼接到一起，再查询
        String bProvinceSeek = (String) map.get("bProvinceSeek");
        String bCitySeek = (String)map.get("bCitySeek");
        String bRegionSeek = (String)map.get("bRegionSeek");
        String bPosition = "";
        if(bProvinceSeek !=null && !"".equals(bProvinceSeek)){
            bPosition =bProvinceSeek;
        }
        if(bCitySeek !=null && !"".equals(bCitySeek)){
            bPosition =bPosition+"|"+bCitySeek;
        }
        if(bRegionSeek !=null && !"".equals(bRegionSeek)){
            bPosition =bPosition+"|"+bRegionSeek;
        }

        map.put("bPosition", bPosition);
        List<ProductBaseEdition> productBases = this.productBaseEditionMapper.selectList(map);
        // 将查询到的地址的'|'换位空格，并将地址拆分为省、市、区
        if(productBases != null && productBases.size() > 0) {
            for (int i = 0; i <productBases.size(); i++) {
                String[] positions = productBases.get(i).getbPosition().split("\\|");
                if (positions != null && positions.length > 0) {
                    for (int j = 0; j < positions.length; j++) {
                        switch (j){
                            case 0:
                                // 省
                                productBases.get(i).setbProvince(positions[j]);
                                break;
                            case 1:
                                // 市
                                productBases.get(i).setbCity(positions[j]);
                                break;
                            case 2:
                                // 区
                                productBases.get(i).setbRegion(positions[j]);
                                break;
                        }
                    }
                }
                // 将'|'替换为空格
                productBases.get(i).setbPosition(productBases.get(i).getbPosition().replace("|", " "));
            }
        }
        return new PageResult<ProductBaseEdition>(productBases);
    }

    @Override
    public Map<String, Object> selectMedicinalNamesEditionList(Date eTime) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<String> medicinalNamesEditionList = medicinalEditionMapper.selectNamesList(eTime);

        resultMap.put("entityList", medicinalNamesEditionList);

        return resultMap;

    }

    @Override
    public PageResult<ProductBaseImgEdition> selectListImg(String tProductbaseId, String eTime) {
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ProductBaseImgEdition> productBaseImgEditionsImgs = null;
        try {
            Date date = sdf.parse(eTime);
            productBaseImgEditionsImgs = productBaseImgEditionMapper.selectList(tProductbaseId,date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new PageResult<ProductBaseImgEdition>(productBaseImgEditionsImgs);
    }
}
